package com.quanshu.exchange.ui.base;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.multidex.MultiDexApplication;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.hyphenate.chat.EMClient;
import com.quanshu.exchange.support.cache.DoubleCache;
import com.quanshu.exchange.support.cache.ICache;
import com.quanshu.exchange.support.chat.ChatHelper;
import com.quanshu.exchange.support.entity.UserEntity;
import com.tencent.smtt.sdk.QbSdk;
import com.tmall.wireless.tangram.TangramBuilder;
import com.tmall.wireless.tangram.util.IInnerImageSetter;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends MultiDexApplication {

    public static MyApplication instance;

    public static MyApplication getInstance() {
        return instance;
    }

    private ICache cache;

    public EMClient getEmClient() {
        return EMClient.getInstance();
    }

    private List<String> hasBeenDelFriend = new ArrayList<>();
    private String currentChatUsername = "";

    private String WX_ID = "wx18bf494f8dc4ba2a";
    private String WX_SECRET = "26b564772f02eb0eb2a3bbbafd70847b";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initCache();
        initChat();
        intiTangram();
        Fresco.initialize(this);
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {

            }

            @Override
            public void onViewInitFinished(boolean b) {

            }
        });
    }

    public void initChat() {
        ChatHelper.getInstance().init(instance);
    }

    private void initCache() {
        cache = new DoubleCache(this);
    }
    private void intiTangram(){
        TangramBuilder.init(this, new IInnerImageSetter() {
            @Override
            public <IMAGE extends ImageView> void doLoadImageUrl(@NonNull IMAGE view, @Nullable String url) {
                Glide.with(instance).load(url).into(view);
            }
        },ImageView.class);
    }

    public UserEntity getCurrentUser() {
        return cache.getCurrentUser();
    }

    /**
     * 保存当前的用户信息
     *
     * @param user 为空则将当前的用户信息置空
     */
    public void setCurrentUser(UserEntity user) {
        cache.setCurrentUser(user);
        if (user != null) {
            cache.setUserName(user.getId() + "");
        } else {
            cache.setUserName("");
        }
    }

    public void setToken(String token) {
        cache.setToken(token);
    }

    public String getToken() {
        if (cache != null) {
            return cache.getToken();
        }
        return null;
    }

    public ICache getCache() {
        return cache;
    }

    public String getWX_ID() {
        return WX_ID;
    }

    public String getWX_SECRET() {
        return WX_SECRET;
    }

    public void addHasBeenDelFriend(String username) {
        if (!hasBeenDelFriend.contains(username)) {
            hasBeenDelFriend.add(username);
        }
    }

    public void removeHasBeenDelFriend(String username) {
        if (hasBeenDelFriend != null) {
            if (hasBeenDelFriend.contains(username)) {
                hasBeenDelFriend.remove(username);
            }
        }
    }

    public List<String> getHasBeenDelFriend() {
        return hasBeenDelFriend;
    }

    public String getCurrentChatUsername() {
        return currentChatUsername;
    }

    public void setCurrentChatUsername(String currentChatUsername) {
        this.currentChatUsername = currentChatUsername;
    }
}
